#!/bin/bash
#
# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#

TS_TOPDIR="${0%/*}/../.."
TS_DESC="fromfile"

. "$TS_TOPDIR"/functions.sh
ts_init "$*"

TESTPROG="$TS_HELPER_LIBSMARTCOLS_FROMFILE"
ts_check_test_command "$TESTPROG"

### Number
#
prefix="number"
declare -A FILTERS

FILTERS["is"]='NUM'
FILTERS["neg"]='!NUM'
FILTERS["and"]='NUM && NAME'
FILTERS["or"]='NUM || NAME'

FILTERS["eq"]='NUM == 100'
FILTERS["ne"]='NUM != 100'

FILTERS["gt"]='NUM > 8000'
FILTERS["ge"]='NUM >= 100'
FILTERS["lt"]='NUM < 100'
FILTERS["le"]='NUM <= 100'

FILTERS["expr-and-expr"]='NUM > 10 && NUM < 8000'
FILTERS["expr-or-expr"]='NUM == 3 || NUM == 100'
FILTERS["and-or"]='(NUM >= 3 && NUM <= 100) || NUM == 0'

FILTERS["as-string"]='NUM == "100"'

printf '%s\n' "${!FILTERS[@]}" | sort | while read name; do
	ts_init_subtest "$prefix-$name"
	echo "expr: ${FILTERS[$name]}" >> $TS_OUTPUT
	echo >> $TS_OUTPUT
	ts_run $TESTPROG --nlines 10 --width 80 \
		--filter "${FILTERS[$name]}" \
		--column $TS_SELF/files/col-name \
		--column $TS_SELF/files/col-number \
		$TS_SELF/files/data-string \
		$TS_SELF/files/data-number \
	>> $TS_OUTPUT 2> /dev/null
	ts_finalize_subtest
done
FILTERS=()


### Float
#
prefix="float"
declare -A FILTERS

FILTERS["is"]='NUM'
FILTERS["neg"]='!NUM'
FILTERS["and"]='NUM && NAME'
FILTERS["or"]='NUM || NAME'

FILTERS["eq"]='NUM == 100'
FILTERS["eq2"]='NUM == 100.5'

FILTERS["ne"]='NUM != 100'
FILTERS["ne2"]='NUM != 100.5'

FILTERS["gt"]='NUM > 8000'
FILTERS["gt2"]='NUM > 8000.5'

FILTERS["ge"]='NUM >= 100'
FILTERS["ge2"]='NUM >= 100.5'

FILTERS["lt"]='NUM < 100'
FILTERS["lt2"]='NUM < 100.5'

FILTERS["le"]='NUM <= 100'
FILTERS["le2"]='NUM <= 100.5'

FILTERS["as-string"]='NUM == "100"'
FILTERS["as-string2"]='NUM == "100.5"'

printf '%s\n' "${!FILTERS[@]}" | sort | while read name; do
	ts_init_subtest "$prefix-$name"
	echo "expr: ${FILTERS[$name]}" >> $TS_OUTPUT
	echo >> $TS_OUTPUT
	ts_run $TESTPROG --nlines 10 --width 80 \
		--filter "${FILTERS[$name]}" \
		--column $TS_SELF/files/col-name \
		--column $TS_SELF/files/col-float \
		$TS_SELF/files/data-string \
		$TS_SELF/files/data-float \
	>> $TS_OUTPUT 2> /dev/null
	ts_finalize_subtest
done
FILTERS=()

### Boolean
#
prefix="bool"
declare -A FILTERS

FILTERS["is"]='BOOL'
FILTERS["neg"]='!BOOL'
FILTERS["and"]='BOOL && NAME'
FILTERS["or"]='BOOL || NAME'

FILTERS["eq-true"]='BOOL == TRUE'
FILTERS["eq-true2"]='BOOL == true'
FILTERS["eq-true3"]='BOOL == 1'

FILTERS["eq-false"]='BOOL == FALSE'
FILTERS["eq-false2"]='BOOL == false'
FILTERS["eq-false3"]='BOOL == 0'

FILTERS["ne"]='BOOL != false'
FILTERS["ne2"]='BOOL != true'

FILTERS["as-string"]='BOOL == "0"'
FILTERS["as-string2"]='BOOL == "1"'

printf '%s\n' "${!FILTERS[@]}" | sort | while read name; do
	ts_init_subtest "$prefix-$name"
	echo "expr: ${FILTERS[$name]}" >> $TS_OUTPUT
	echo >> $TS_OUTPUT
	ts_run $TESTPROG --nlines 10 --width 80 \
		--filter "${FILTERS[$name]}" \
		--column $TS_SELF/files/col-name \
		--column $TS_SELF/files/col-bool \
		$TS_SELF/files/data-string \
		$TS_SELF/files/data-bool \
	>> $TS_OUTPUT 2> /dev/null
	ts_finalize_subtest
done
FILTERS=()


### Strings
#
prefix="string"
declare -A FILTERS

FILTERS["is"]='NAME'
FILTERS["neg"]='!NAME'

FILTERS["eq"]='NAME == "bbb"'
FILTERS["ne"]='NAME != "bbb"'

FILTERS["gt"]='NAME > "bbb"'
FILTERS["ge"]='NAME >= "bbb"'
FILTERS["lt"]='NAME < "bbb"'
FILTERS["le"]='NAME <= "bbb"'

FILTERS["reg"]='NAME =~ "aaa.*"'
FILTERS["nreg"]='NAME !~ "aaa.*"'

printf '%s\n' "${!FILTERS[@]}" | sort | while read name; do
	ts_init_subtest "$prefix-$name"
	echo "expr: ${FILTERS[$name]}" >> $TS_OUTPUT
	echo >> $TS_OUTPUT
	ts_run $TESTPROG --nlines 10 --width 80 \
		--filter "${FILTERS[$name]}" \
		--column $TS_SELF/files/col-name \
		$TS_SELF/files/data-string \
	>> $TS_OUTPUT 2> /dev/null
	ts_finalize_subtest
done
FILTERS=()


### Broken
#
prefix="broken"
declare -A FILTERS

FILTERS["no-start"]='NUM == 100)'
FILTERS["no-close"]='(NUM == 100'
FILTERS["no-column"]=' == 100'
FILTERS["no-param"]='NUM == '
FILTERS["bad-oper"]='NUM ** 100'
FILTERS["bad-oper2"]='NUM = 100'
FILTERS["bad-column"]='XXX == 100'
FILTERS["bad-number"]='NUM == 1y0'
FILTERS["bad-float"]='NUM == 100 . 5'

printf '%s\n' "${!FILTERS[@]}" | sort | while read name; do
	ts_init_subtest "$prefix-$name"
	echo "expr: ${FILTERS[$name]}" >> $TS_OUTPUT
	echo >> $TS_OUTPUT
	ts_run $TESTPROG --nlines 10 --width 80 \
		--filter "${FILTERS[$name]}" \
		--column $TS_SELF/files/col-number \
		$TS_SELF/files/data-number \
	2>> $TS_OUTPUT 1> /dev/null

	sed -i -e 's/syntax error.*/<bison syntax error message removed>/g' $TS_OUTPUT
	ts_finalize_subtest
done
FILTERS=()


ts_log "...done."
ts_finalize
